/* **************************************************************************																					
 *                                OpenPicus                 www.openpicus.com
 *                                                            italian concept
 * 
 *            openSource wireless Platform for sensors and Internet of Things	
 * **************************************************************************
 *  FileName:        nimbits.c
 *  Dependencies:    Microchip configs files
 *  Module:          FlyPort WI-FI
 *  Compiler:        Microchip C30 v3.12 or higher
 *
 *  Author               Rev.    Date              Comment
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  Stefano Saccucci     1.0     10/22/2011		   First release  (core team)
 *  					  www.notonlyelectronic.com
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  Software License Agreement
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  This is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License (version 2) as published by 
 *  the Free Software Foundation AND MODIFIED BY OpenPicus team.
 *  
 *  ***NOTE*** The exception to the GPL is included to allow you to distribute
 *  a combined work that includes OpenPicus code without being obliged to 
 *  provide the source code for proprietary components outside of the OpenPicus
 *  code. 
 *  OpenPicus software is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 *  more details. 
 * 
 * 
 * Warranty
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
 *
 **************************************************************************/

#include "nimbits.h"

/****************************************************************************************
 *				       			--- Global Parameters ---			                    *
 ***************************************************************************************/	

static TCP_SOCKET Socket;
static BOOL flagTCP=FALSE;
static BOOL flagTCPisCON=FALSE;
static int z=0;


  /*---------------------------------------------------------------------------------------------------------
  |	Function: 		wfNIMBITS(char* hostappspot, char* email, char* namevalue, float value)	|
  | Description: 	Function to write float data in Nimbits server.											|
  | Returns:		-														   								|
  | Parameters:		char* hostappspot - personal host appspot -> myhost.appspot.com							|
  |																		 									|					
  |					char* email - email to enter on your server			 									|
  | 																										|
  |					char* namevalue - name of value to send				 									|
  | 																										|
  |					float value - float data to send to Nimbits  	  	  					  	|
  | 																										|
|					Example: namevalue="Temperature" , value=25 .        								|
  ---------------------------------------------------------------------------------------------------------*/

void wfNIMBITS(char* hostappspot, char* email, char* secret, char* namevalue, float* pvalue)
{	
	char *strPOST=malloc(200);
		
	flagTCP=FALSE;
	
	int iLenStrPost=sprintf( strPOST, "GET /service/currentvalue?value=%2.1f&point=%s&email=%s&format=double&secret=%s HTTP/1.1\r\nHost:%s\r\nContent-Length:0\r\n\r\n",(double)pvalue[0],namevalue, email, secret, hostappspot);
	
	while(flagTCP==FALSE)
		{
			if(z==5)
			{	
				z=0;
				flagTCP=TRUE;
			}
			else
			{	
				Socket=TCPClientOpen ( hostappspot, "80");
				vTaskDelay(25);
				flagTCP=TCPisConn(Socket);
				flagTCPisCON=flagTCP;
				vTaskDelay(25);
				z++;
			}
		}
		if(flagTCPisCON==TRUE)
		{
			TCPWrite ( Socket, strPOST, iLenStrPost );
			UARTWrite(1,">");
			vTaskDelay(200);
			TCPClientClose ( Socket );
			flagTCPisCON=FALSE;
		}
		free(strPOST);
		return;
	}
